package com.base.param;

import cn.hutool.core.collection.CollUtil;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created on 2023/7/5.
 *
 * @author zhaojb
 */
@Setter
@Getter
@Accessors(chain = true)
public class SelectSqlParam extends BaseSqlParam {

    private List<WhereParam> whereParams;
    private LimitParam limitParam;
    private SelectItem selectItem = new SelectItem();
    private List<OrderParam> orderParams;

    public SelectSqlParam(String tableName) {
        super(tableName);
    }

    @Data
    public static class SelectItem {
        private List<SelectItemParam> selectColumns;
        /**
         * 默认查询全部字段
         */
        private boolean selectAll = true;

        public SelectItem withSelectColumns(List<String> selectColumnCodes) {
            if (CollUtil.isEmpty(selectColumnCodes)) {
                return this;
            }
            this.selectAll = false;
            this.selectColumns = selectColumnCodes.stream().map(code -> new SelectItemParam(code)).collect(Collectors.toList());
            return this;
        }

        public void setSelectColumns(List<SelectItemParam> selectColumns) {
            this.selectColumns = selectColumns;
            this.selectAll = false;
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class OrderParam {
        private String columnCode;
        private SqlOrderKeyword orderKeyword;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class LimitParam {
        private int pageIndex;
        private int pageSize;
    }

}
